package com.lemon.smartsafetynew.ui.adapter.device;

import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.lemon.smartsafety.R;
import com.lemon.smartsafety.listener.RecyclerTClickListener;
import com.lemon.smartsafetynew.entity.device.DeviceVo;
import com.lemon.smartsafetynew.util.common.CommonUtil;
import com.lemon.smartsafetynew.util.device.DeviceUtils;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import kale.adapter.LemonRcvAdapter;
import kale.adapter.item.AdapterItem;

/**
 * Created by Andrew on 2017/9/15.
 */

public class DeviceListAdapter extends LemonRcvAdapter<DeviceVo> {

    private RecyclerTClickListener<DeviceVo> mListener;

    public DeviceListAdapter(@Nullable List<DeviceVo> data,
                             RecyclerTClickListener<DeviceVo> mListener) {
        super(data);
        this.mListener = mListener;

    }

    @NonNull
    @Override
    public AdapterItem createItem(Object type) {
        return new ItemAdapter();
    }

    public class ItemAdapter implements AdapterItem<DeviceVo> {

        private DeviceVo mData;
        private int mPosition;

        @Bind(R.id.layout)
        LinearLayout layout;
        @Bind(R.id.device_name_tv)
        TextView deviceNameTv;
        @Bind(R.id.device_number_tv)
        TextView deviceNumberTv;
        @Bind(R.id.device_status)
        TextView deviceStatus;

        @Override
        public int getLayoutResId() {
            return R.layout.item_device;
        }

        @Override
        public void bindViews(View root) {
            ButterKnife.bind(this, root);
        }

        @Override
        public void setViews() {
            layout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mListener.onItemClick(mData, mPosition);
                }
            });
        }

        @Override
        public void handleData(DeviceVo deviceVo, int position) {

            mData = deviceVo;
            mPosition = position;

            deviceNameTv.setText("设备名称：" + CommonUtil.avoidNullString(deviceVo.getName()));
            deviceNumberTv.setText("设备编码：" + CommonUtil.avoidNullString(deviceVo.getCode()));

            int id = DeviceUtils.getStatus(deviceVo.getStatus());
            deviceStatus.setBackgroundResource(DeviceUtils.deviceStatusBgRes[id]);
            deviceStatus.setText(DeviceUtils.deviceStatus[id]);

        }
    }
}
